/*
 * TMap Designer - A Thematic Map design software
 * Copyright (C) 2006 World Health Organization
 * http://www.who.int
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
package who.tmap.toolkit.dao;

import java.awt.Image;
import java.io.File;

import javax.swing.ImageIcon;

import org.apache.log4j.Logger;
import org.jdom.Attribute;
import org.jdom.Element;

import who.tmap.dao.TMapDAOException;
import who.tmap.toolkit.shape.MapShape;
import who.tmap.toolkit.shape.MapShapes;
import who.tmap.toolkit.shape.annotation.BackgroundImageShape;

/**
 * DAO for Background image annotations
 * @author Administrator
 *
 */
public class BackgroundImageShapeDAO extends BaseMapShapeDAO {

    private static Logger logger = Logger.getLogger(BackgroundImageShapeDAO.class);	
	
	public MapShape load( Element elt ) throws TMapDAOException {
		BackgroundImageShape bis =null;
		
		try {
	    	bis =(BackgroundImageShape)msf.getGraphic( MapShapes.BACKGROUND_IMAGE );
	    	
	    	Attribute fileAttr =elt.getAttribute( "linkFile" );
	    	if ( fileAttr != null ) {
	    		String fileName =fileAttr.getValue();
	    		
	    		bis.loadImageFile( new File( fileName ) );
	    	} else {	   
	    		byte[] imageData =null;
	    		
	    		Element inlineElt =elt.getChild( "Inline" );
	    		imageData =miscHelper.loadInlineImage( inlineElt != null ? inlineElt : elt );
	    		
	    		bis.setImageBytes( imageData );
	        	ImageIcon imageIcon =new ImageIcon( imageData );
		        bis.setImage( imageIcon.getImage() );
	    	}
        
		} catch( Exception e ) {
			throw new TMapDAOException( e );
		}
        
        return bis;
	}
	
	public Element save( MapShape mapShape ) throws TMapDAOException {
		BackgroundImageShape bis =(BackgroundImageShape)mapShape;
		
		Element bisElt =new Element( "BackgroundImage" );
		
		Image img =bis.getImage();

		if ( bis.getFile() != null ) {
			bisElt.setAttribute( "linkFile", bis.getFile().getAbsolutePath() );
		} else if ( img != null ) {
			bisElt.setAttribute( "width", Integer.toString( img.getWidth(null) ) );
			bisElt.setAttribute( "height", Integer.toString( img.getHeight(null) ) );    
			
			Element inlineElt =new Element( "Inline" );
			bisElt.addContent( inlineElt );
			inlineElt.setText( miscHelper.saveInlineImage( bis.getImageBytes() ) );
		}
		
		return bisElt;
	}	
	
}
